package com.kye.express.business.personal_my_bill.monthly_balance_bill.list;

import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.util.TypedValue;

import com.jakewharton.rxbinding2.view.RxView;
import com.kye.base.DebounceObservableTransformer;
import com.kye.express.R;
import com.kye.express.business.base.BaseAppCompatActivity;
import com.kye.express.databinding.ActivityMyBillListBinding;
import com.kye.express.utils.ToolbarManager;
import com.kye.lib.view.statuslayout.StatusLayout;
import com.kye.model.bean.bill.MonthlyBalanceBillBean;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import java.util.List;

import io.reactivex.functions.Consumer;
import me.drakeet.multitype.MultiTypeAdapter;

public class MyBillListActivity extends BaseAppCompatActivity implements MyBillListContract.MyBillListView {

    public static final int REQUEST_CODE_MONTHLY_BALANCE_BILL_DETAIL = 1;

    private ActivityMyBillListBinding mBinding;
    private MultiTypeAdapter mAdapter;
    private MyBillListContract.MyBillListPresenter mPresenter;
    private MonthlyBalanceBillViewBinder mBinder;

    public static void startActivity(Context context) {
        context.startActivity(new Intent(context, MyBillListActivity.class));
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_my_bill_list);

        initView();
        initEvent();
        initData();
    }

    private void initView() {
        mBinding.recyclerViewContent.setLayoutManager(new LinearLayoutManager(this));
        mBinding.recyclerViewContent.addItemDecoration(new HorizontalDividerItemDecoration.Builder(this)
                .color(android.R.color.transparent)
                .size((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10F, getResources().getDisplayMetrics()))
                .build());
        mAdapter = new MultiTypeAdapter();
        mAdapter.setHasStableIds(true);
        mBinder = new MonthlyBalanceBillViewBinder();
        mAdapter.register(MonthlyBalanceBillBean.class, mBinder);
        mBinding.recyclerViewContent.setAdapter(mAdapter);
    }

    private void initEvent() {
        ToolbarManager.setToolbar(this, mBinding.toolbar);

        RxView.clicks(mBinding.linearLayoutErrorNet.btErrorNetRetry)
                .compose(new DebounceObservableTransformer<>())
                .compose(bindToLifecycle())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        mBinding.statusLayout.showLoadingView();
                    }
                });

        RxView.clicks(mBinding.linearLayoutErrorServer.btErrorServerRetry)
                .compose(new DebounceObservableTransformer<>())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        mBinding.statusLayout.showLoadingView();
                    }
                });

        RxView.clicks(mBinding.linearLayoutEmpty.btEmptyRetry)
                .compose(new DebounceObservableTransformer<>())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        mBinding.statusLayout.showLoadingView();
                    }
                });

        mBinding.statusLayout.setOnLoadingListener(new StatusLayout.OnLoadingListener() {
            @Override
            public void onLoading() {
                mPresenter.loadData();
            }
        });
    }

    private void initData() {
        mPresenter = new MyBillListContract.MyBillListPresenter();
        mPresenter.bind(this);

        mBinding.statusLayout.showLoadingView();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_MONTHLY_BALANCE_BILL_DETAIL && resultCode == RESULT_OK) {
            mBinding.statusLayout.showLoadingView();
        }
    }

    @Override
    public void onError() {
        mBinding.statusLayout.showErrorNetworkView();
    }

    @Override
    public void onError(String msg) {
        mBinding.statusLayout.showErrorServerView();
    }

    @Override
    public void onLoading() {
        mBinding.statusLayout.showLoadingView();
    }

    @Override
    public void onLoaded() {

    }

    @Override
    public void loadedData(List<MonthlyBalanceBillBean> billBeans) {
        mBinding.statusLayout.showContentView();

        mAdapter.setItems(billBeans);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onEmpty(String msg) {
        mBinding.statusLayout.showEmptyView();
    }
}
